/**
 * 
 */
package org.jenkon.filter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Filter composition for compare with and.
 * 
 * @author jkonopka
 * 
 * @param <T> the type of the filter
 */
public class AndFilter<T> implements Filter<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6520773085615925954L;

	private final List<Filter<T>> delegates = new ArrayList<Filter<T>>();

	/**
	 * 
	 */
	public AndFilter() {
		this(new ArrayList<Filter<T>>());
	}

	/**
	 * @param delegates {@link List} with delegating {@link Filter}
	 */
	public AndFilter(final List<Filter<T>> delegates) {
		this.add(delegates);
	}

	/**
	 * @param delegates {@link List} with delegating {@link Filter}
	 */
	public void add(final List<Filter<T>> delegates) {
		this.delegates.addAll(delegates);
	}

	/**
	 * @param delegates list with delegating {@link Filter}
	 */
	public void add(final Filter<T>... delegates) {
		this.add(Arrays.asList(delegates));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jenkon.commons.lang.filter.Filter#accept(java.lang.Object)
	 */
	public boolean accept(final T value) {
		for (final Filter<T> filter : delegates) {
			if (!filter.accept(value)) {
				return false;
			}
		}
		return true;
	}

}
